<?php
class AppController extends Controller{
	var $view = 'Theme';
    var $theme = HOST;
	
	var $uses = array('Cm','CmsSection','User');
	var $permissions = array();
	var $helpers = array('Session','Html','Javascript','Form','Time','Tree','Js' => array('Jquery'),'Cksource');
    var $components = array( 'Auth','Session','RequestHandler','Email','Cookie','CompCms','Waluty','EuroDate','Series','Search.Prg');
    
    
	
    /**
     * (non-PHPdoc)
     * @see cake/libs/controller/Controller::beforeFilter()
     */
    function beforeFilter() {
		parent::beforeFilter();
		$this->_setLanguage();

    	
		//read cms settings for page
		$this->CompCms->get();
		
        
		if(!$this->Auth->user()) if($this->isAuthorized()) $this->Auth->allow($this->action);
		
		$this->Auth->authorize = 'controller';
        $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
        $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');
	}
    
	function _setLanguage() {
		if(!isset($this->params['language'])){
			if ($this->Cookie->read('Config.lang') && !$this->Session->check('Config.language')) {
				$this->Session->write('Config.language', $this->Cookie->read('Config.lang'));
				Configure::write('Config.language', $this->Cookie->read('Config.lang'));
			}else if ($this->Session->read('Config.language') != false) {     
				Configure::write('Config.language', $this->Session->read('Config.language'));				
			}else{
				// domyślny język
				$default_lang = Configure::read('Config.default_lang');
				if (array_key_exists(HOST,$default_lang)) {			
				    Configure::write('Config.language', $default_lang[HOST]);
				}else{
					Configure::write('Config.language', 'pol');
				}
			}
		}else if(isset($this->params['language']) && $this->params['language'] != Configure::read('Config.language')){
			Configure::write('Config.language', $this->params['language']);
			$this->Session->write('Config.language',$this->params['language']);	
			$this->Cookie->write('Config.lang',$this->params['language']);			
		}else{			
			// domyślny język
			$default_lang = Configure::read('Config.default_lang');
			if (array_key_exists(HOST,$default_lang)) {			
			    Configure::write('Config.language', $default_lang[HOST]);
			}else{
				Configure::write('Config.language', 'pol');
			}
		}
	}
	
    function isAuthorized(){    	
    	
        if($this->Auth->user('group') == 'admin') return true; //Remove this line if you don't want admins to have access to everything by default 
        if(!empty($this->permissions[$this->action])){ 
           	if($this->permissions[$this->action] == '*') return true;            	
            if(in_array($this->Auth->user('group'), $this->permissions[$this->action])) return true;            
        }else{ 
		    // If there are no permissions, let everyone in 		    
		    $this->Auth->allow($this->action); 
		    return true; 
		}
		
        return false;         
    }
}